package com.inplaceedit.shared.webservice.impl;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.inplaceedit.shared.dto.CompanyDto;
import com.inplaceedit.shared.dto.ContactDto;
import com.inplaceedit.shared.dto.DtoFactory;
import com.inplaceedit.shared.dto.NoteDto;
import com.inplaceedit.shared.dto.PhoneDto;
import com.inplaceedit.shared.webservice.impl.data.Company;
import com.inplaceedit.shared.webservice.impl.data.Contact;
import com.inplaceedit.shared.webservice.impl.data.Data;
import com.inplaceedit.shared.webservice.impl.data.Note;
import com.inplaceedit.shared.webservice.impl.data.Phone;

public class Converter {
	private static DtoFactory factory = GWT.create(DtoFactory.class);

	public static CompanyDto convert(final Company from) {
		CompanyDto to = null;
		if (from != null) {
			to = factory.createCompany().as();
			to.setId(from.getId());
			to.setName(from.getName());
		}
		return to;
	}

	public static Company convert(final CompanyDto from) {
		Company to = null;
		if (from != null) {
			if (from.getId() != null) {
				to = Data.get().getCompany(from.getId());
			}
			if (to == null) {
				to = new Company();
				to.setId(from.getId());
			}
			to.setName(from.getName());
		}
		return to;
	}

	public static ContactDto convert(final Contact from) {
		ContactDto to = null;
		if (from != null) {
			to = factory.createContact().as();
			to.setId(from.getId());
			to.setFirstName(from.getFirstName());
			to.setLastName(from.getLastName());
			to.setTitle(from.getTitle());
			to.setCompany(convert(Data.get().getCompany(from.getCompanyId())));
			to.setBirthday(from.getBirthday());
			to.setSex(convert(from.getSex()));
			to.setPhoneList(convertPhoneList(from.getPhoneList()));
		}
		return to;
	}

	public static ContactDto.Sex convert(final Contact.Sex from) {
		ContactDto.Sex to = null;
		if (from != null) {
			switch (from) {
			case MALE:
				to = ContactDto.Sex.MALE;
				break;
			case FEMALE:
				to = ContactDto.Sex.FEMALE;
				break;
			default:
				to = ContactDto.Sex.OTHER;
				break;
			}
		}
		return to;
	}

	public static Contact convert(final ContactDto from) {
		Contact to = null;
		if (from != null) {
			if (from.getId() != null) {
				to = Data.get().getContact(from.getId());
			}
			if (to == null) {
				to = new Contact();
				to.setId(from.getId());
			}
			to.setFirstName(from.getFirstName());
			to.setLastName(from.getLastName());
			to.setTitle(from.getTitle());
			to.setCompanyId(from.getCompany() != null ? from.getCompany()
					.getId() : null);
			to.setBirthday(from.getBirthday());
			to.setSex(convert(from.getSex()));
			to.setPhoneList(convertPhoneDtoList(from.getPhoneList()));
		}
		return to;
	}

	public static Contact.Sex convert(final ContactDto.Sex from) {
		Contact.Sex to = null;
		if (from != null) {
			switch (from) {
			case MALE:
				to = Contact.Sex.MALE;
				break;
			case FEMALE:
				to = Contact.Sex.FEMALE;
				break;
			default:
				to = Contact.Sex.OTHER;
				break;
			}
		}
		return to;
	}

	public static NoteDto convert(final Note from) {
		NoteDto to = null;
		if (from != null) {
			to = factory.createNote().as();
			to.setId(from.getId());
			to.setTime(from.getTime());
			to.setText(from.getText());
			if (from.getContactId() != null) {
				to.setContact(convert(Data.get()
						.getContact(from.getContactId())));
			}
		}
		return to;
	}

	public static Note convert(final NoteDto from) {
		Note to = null;
		if (from != null) {
			if (from.getId() != null) {
				to = Data.get().getNote(from.getId());
			}
			if (to == null) {
				to = new Note();
				to.setId(from.getId());
			}
			to.setTime(from.getTime());
			to.setText(from.getText());
			if (from.getContact() != null) {
				to.setContactId(from.getContact().getId());
			}
		}
		return to;
	}

	public static PhoneDto convert(final Phone from) {
		PhoneDto to = null;
		if (from != null) {
			to = factory.createPhone().as();
			to.setType(convert(from.getType()));
			to.setNumber(from.getNumber());
		}
		return to;
	}

	public static PhoneDto.Type convert(final Phone.Type from) {
		PhoneDto.Type to = null;
		if (from != null) {
			switch (from) {
			case HOME:
				to = PhoneDto.Type.HOME;
				break;
			case BUSINESS:
				to = PhoneDto.Type.BUSINESS;
				break;
			case CELL:
				to = PhoneDto.Type.CELL;
				break;
			}
		}
		return to;
	}

	public static Phone convert(final PhoneDto from) {
		Phone to = null;
		if (from != null) {
			to = new Phone();
			to.setType(convert(from.getType()));
			to.setNumber(from.getNumber());
		}
		return to;
	}

	public static Phone.Type convert(final PhoneDto.Type from) {
		Phone.Type to = null;
		if (from != null) {
			switch (from) {
			case HOME:
				to = Phone.Type.HOME;
				break;
			case BUSINESS:
				to = Phone.Type.BUSINESS;
				break;
			case CELL:
				to = Phone.Type.CELL;
				break;
			}
		}
		return to;
	}

	public static List<Phone> convertPhoneDtoList(final List<PhoneDto> from) {
		List<Phone> to = null;
		if (from != null) {
			to = new ArrayList<Phone>();
			for (final PhoneDto item : from) {
				to.add(convert(item));
			}
		}
		return to;
	}

	public static List<PhoneDto> convertPhoneList(final List<Phone> from) {
		List<PhoneDto> to = null;
		if (from != null) {
			to = new ArrayList<PhoneDto>();
			for (final Phone item : from) {
				to.add(convert(item));
			}
		}
		return to;
	}
}
